package com.foreknow.elm.service.impl;


import com.foreknow.elm.dao.CartDao;

import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    /**
     * 功能：根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * ​ 并获取自动生成的订单编号，
     * ​ 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
     * ​ 然后根据用户编号、商家编号删除购物车表中的数据。
     */
    @Override
    public int createOrders(String userId, Integer businessId, int daId, double orderTotal) {
        Integer orderId = 0;
        try {
            //开启事务
            DBUtil.getConnection();
//        1. 查询购物车列表
            Cart cart = new Cart();
            cart.setUserId(userId);
            cart.setBusinessId(businessId);
            CartService service = new CartServiceImpl();
            List<Cart> list = service.listCart(cart);
//        2. 创建订单并返回订单的编号
            OrdersDao ordersDao = new OrdersDaoImpl();
            ordersDao.createOrders(userId,businessId,daId,orderTotal);
         //3. 向明细表中批量添加数据
            List<Orderdetailet> list1 = new ArrayList<>();
            //遍历list
            //每循环一次就可以获取到一个Cart购物车对象
            //创建一个明细对象并给该对象的属性赋值(orderId,foodId,quantity)
            // 将明细对象添加到list集合中
            for (int i=0;i<list.size();i++){
                Cart cart1 = list.get(i);
               Orderdetailet odl = new Orderdetailet();
                odl.setOrderId(orderId);
                odl.setFoodId(cart1.getFoodId());
                odl.setQuantity(cart1.getQuantity());
                list1.add(odl);
                //调用明细方法
                OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
                orderdetailetDao.saveOrderDetailetBath(list1);

                //4. 清空购物车
                service.removeCart(cart);
                //提交事务或回滚事务
                DBUtil.commitTransaction();
            }
        } catch (Exception e) {

        try {
            DBUtil.rollbackTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        e.printStackTrace();
    }
//
      return orderId;
    }
    /**
     * 功能：根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     * @param orderId
     * @return
     */
    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders=new Orders();
        OrdersDao ordersDao=new OrdersDaoImpl();
       OrderdetailetDao orderDetailetDao=new OrderdetailetDaoImpl();
        try {
            orders= ordersDao.getOrdersById(orderId);
            List<Orderdetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
            //设置关联关系
            orders.setOrderdetailetList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }

    /**
     * 功能：根据用户编号查询此用户的所有订单信息
     * @param userId
     * @return
     */
    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list=new ArrayList<>();
        OrdersDao ordersDao=new OrdersDaoImpl();
        try {
            list= ordersDao.listOrdersByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

}
